/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queryprocessing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import queryterm.QueryTerm;

/**
 *
 * @author Zakiy Firdaus
 */
public class Tokenizer {

    public Tokenizer() {
    }
    public static long ORIGINAL_WEIGHT = (long) 1;
    public static long STEMMED_WEIGHT = (long) 0.5;
    public static long SYNONYM_WEIGHT = (long) 0.25;
    public static long RELATED_WEIGHT = (long) 0.25;
    public static long SPLITTED_WEIGHT = (long) 1; //dibagi n

    public static ArrayList<QueryTerm> tokenize(String query) {
        ArrayList<QueryTerm> lqt = new ArrayList<QueryTerm>();
        HashMap<String, QueryTerm> hmqt = new HashMap<String, QueryTerm>();
        
        query = lowerCase(query);
        ArrayList<String> aQuery = split(query);
        boolean oneword;

        for (String s : aQuery) {

            String[] splittedQ = s.split(":");
            String token = splittedQ[0];
            token = processQuery(token);
            int field = QueryTerm.All_FIELD;
            if (splittedQ.length > 1) {
                field = QueryTerm.getFieldInt(splittedQ[1]);
            }
            //            if()
            if (token.contains(" ")) {
                oneword = false;
            } else {
                oneword = true;
            }
            QueryTerm qt = new QueryTerm(token, ORIGINAL_WEIGHT, field, oneword);
            if (hmqt.containsKey(token + "'" + qt.getField())) {
                qt.setWeight(hmqt.get(token + "'" + qt.getField()).getWeight() + qt.getWeight());
            }
            hmqt.put(token + "'" + qt.getField(), qt);
//            lqt.add(qt);

        }
        Iterator it = hmqt.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            lqt.add((QueryTerm)pairs.getValue());
            it.remove();
        }

        return lqt;
    }

    public static ArrayList<String> split(String s) {
        ArrayList<String> als = new ArrayList<String>();
        int idxAwal = -1;
        int idxAkhir = -1;
        while (s.indexOf("\"") >= 0) {
            idxAwal = s.indexOf("\"");
            idxAkhir = s.indexOf("\"", idxAwal + 1);
            if (s.length() > idxAkhir + 1 && s.substring(idxAkhir + 1, idxAkhir + 2).equals(":")) {
                int idxSpace = s.indexOf(" ", idxAkhir + 1);
                if(idxSpace==-1){
                    idxSpace=s.length();
                }
                String splittedraws = s.substring(idxAwal + 1, idxAkhir);
                String splitteds = splittedraws + s.substring(idxAkhir + 1, idxSpace);
                if(idxSpace==s.length()){
                    s = s.substring(0, idxAwal);
                } else {
                    s = s.substring(0, idxAwal) + s.substring(idxSpace + 1);
                }
                
                als.add(splitteds);
            } else {
                String splitteds = s.substring(idxAwal + 1, idxAkhir);
                String temps = "";
                if (idxAwal != 0) {
                    temps = s.substring(0, idxAwal);
                }
                if (s.length() > idxAkhir + 1) {
                    temps += s.substring(idxAkhir + 2);
                }
                s = temps;
                als.add(splitteds);
            }
        }
//        als.add(null);
        String[] aQuery = s.split(" ");
        for (String q : aQuery) {
            if (!q.isEmpty()) {
                als.add(q);
            }
        }
//        for(String ss:als){
//            System.out.println(ss);
//        }
        return als;
    }

    public static String lowerCase(String s) {
        return s.toLowerCase();
    }

    public static String processQuery(String s) {
        //menghapus (-)
        s = s.replaceAll("-", "");
        //menghapus ('s) yang menerangkan kepemilikan
        if (s.length() >= 2) {
            if (s.substring(s.length() - 2).equals("'s")) {
                s = s.substring(0, s.length() - 2);
            }
        }
        //menghapus (')
        s = s.replaceAll("'", "");

        return s;
    }

//    public static void main(String[] args) {
////        Tokenizer tar = new Tokenizer();
//        String query = "O'Donnel's:from novel \"tes 123\":subject books:body \"Read write read\"";
//        query = "\"outlook migration   team\":body team:subject team:subject team:body";
//        System.out.println("query: " + query);
//        System.out.println("--");
//        System.out.println("TOKEN:");
//
//        ArrayList<QueryTerm> lqt = Tokenizer.tokenize(query);
//        for (QueryTerm qt : lqt) {
//            String token = qt.getToken();
//            String field = qt.getFieldString();
//            boolean oneword = qt.getIsOneWord();
//            float weight = qt.getWeight();
//            System.out.println("> [" + token + "] (field:" + field + ", weight:" + weight + ", isOneWord:" + oneword + ")");
//        }
//        
//        System.out.println("");
//        System.out.println("--");
////        System.out.println("EXTENDED TOKEN:");
////        System.out.println("");
////        lqt = QueryExpansion.expand(lqt);
////        QueryExpansion.sortListQuery(lqt);
////        for (QueryTerm qt : lqt) {
////            String token = qt.getToken();
////            String field = qt.getFieldString();
////            boolean oneword = qt.getIsOneWord();
////            float weight = qt.getWeight();
////            System.out.println("> " + token + " (field:" + field + ", weight:" + weight + ", isOneWord:" + oneword + ")");
////        }
//    }
}
